import DateUtil from "../../engine/DateUtil";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { ViewConfig } from "../config/ViewConfig";
import { RoomPlayer } from "../model/RoomPlayer";
import { UIHelper } from "../util/UIHelper";
import ViewShop from "./ViewShop";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewCheckChargeJinbi extends ViewBase {

    private btnCancel: cc.Node

    private timeNode: cc.Node;
    private lbLeftTime: cc.Label
    private endTime: number

    public onInitChild(endTime: number): void {
        this.endTime = endTime;
    }

    private init() {
        if (!this.isInit) {
            this.isInit = true;
            this.timeNode = this.findChild(this.node, "timeNode")
            this.lbLeftTime = this.getChild(this.timeNode, "lbLeftTime", cc.Label)
            let controlUI: cc.Node = this.findChild(this.node, "controlUI")
            let btnOK: cc.Node = this.findChild(controlUI, "btnOK");
            UIHelper.onAddEvent(btnOK, () => {
                UIManager.instance.showView(ViewConfig.viewConfig.ViewShop, () => {
                    this.closeView()
                }, ViewShop.PanelType.Ziyouchang)
            })
            this.btnCancel = this.findChild(controlUI, "btnCancel");
            UIHelper.onAddEvent(this.btnCancel, () => {
                this.closeView()
                SocketClient.req_ziyou_release_room_no_charge()
            })
            let btnClose: cc.Node = this.findChild(this.node, "btnClose");
            UIHelper.onAddEvent(btnClose, () => {
                this.closeView()
                SocketClient.req_ziyou_release_room_no_charge()

            })
        }
    }

    onEnable() {
        this.setTopUI()
        this.init();
    }

    protected update(dt: number): void {
        if (this.timeNode.active) {
            let leftTime: number = this.endTime - DateUtil.now()
            this.lbLeftTime.string = Math.floor(leftTime / 1000) + "秒"
            if (leftTime <= 0) {
                this.closeView()
                SocketClient.req_ziyou_release_room_no_charge()
            }
        }
    }
}
